package cn.iocoder.yudao.module.system.controller.admin.productionplanissued;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.productionplanissued.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.productionplanissued.ProductionPlanIssuedDO;
import cn.iocoder.yudao.module.system.service.productionplanissued.ProductionPlanIssuedService;

@Tag(name = "管理后台 - 生成计划下发批次")
@RestController
@RequestMapping("/system/production-plan-issued")
@Validated
public class ProductionPlanIssuedController {

    @Resource
    private ProductionPlanIssuedService productionPlanIssuedService;

    @PostMapping("/create")
    @Operation(summary = "创建生成计划下发批次")
    @PreAuthorize("@ss.hasPermission('system:production-plan-issued:create')")
    public CommonResult<Integer> createProductionPlanIssued(@Valid @RequestBody ProductionPlanIssuedSaveReqVO createReqVO) {
        return success(productionPlanIssuedService.createProductionPlanIssued(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新生成计划下发批次")
    @PreAuthorize("@ss.hasPermission('system:production-plan-issued:update')")
    public CommonResult<Boolean> updateProductionPlanIssued(@Valid @RequestBody ProductionPlanIssuedSaveReqVO updateReqVO) {
        productionPlanIssuedService.updateProductionPlanIssued(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除生成计划下发批次")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:production-plan-issued:delete')")
    public CommonResult<Boolean> deleteProductionPlanIssued(@RequestParam("id") Integer id) {
        productionPlanIssuedService.deleteProductionPlanIssued(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除生成计划下发批次")
                @PreAuthorize("@ss.hasPermission('system:production-plan-issued:delete')")
    public CommonResult<Boolean> deleteProductionPlanIssuedList(@RequestParam("ids") List<Integer> ids) {
        productionPlanIssuedService.deleteProductionPlanIssuedListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得生成计划下发批次")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:production-plan-issued:query')")
    public CommonResult<ProductionPlanIssuedRespVO> getProductionPlanIssued(@RequestParam("id") Integer id) {
        ProductionPlanIssuedDO productionPlanIssued = productionPlanIssuedService.getProductionPlanIssued(id);
        return success(BeanUtils.toBean(productionPlanIssued, ProductionPlanIssuedRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得生成计划下发批次分页")
    @PreAuthorize("@ss.hasPermission('system:production-plan-issued:query')")
    public CommonResult<PageResult<ProductionPlanIssuedRespVO>> getProductionPlanIssuedPage(@Valid ProductionPlanIssuedPageReqVO pageReqVO) {
        PageResult<ProductionPlanIssuedRespVO> pageResult = productionPlanIssuedService.getProductionPlanIssuedPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ProductionPlanIssuedRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出生成计划下发批次 Excel")
    @PreAuthorize("@ss.hasPermission('system:production-plan-issued:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportProductionPlanIssuedExcel(@Valid ProductionPlanIssuedPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ProductionPlanIssuedRespVO> list = productionPlanIssuedService.getProductionPlanIssuedPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "生成计划下发批次.xls", "数据", ProductionPlanIssuedRespVO.class,
                        BeanUtils.toBean(list, ProductionPlanIssuedRespVO.class));
    }

}
